package fag.editor;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import fag.core.collision.Hitbox;
import fag.core.geometry.Circle2D;
import fag.core.geometry.Polygon2D;
import fag.core.geometry.Rectangle2D;
import fag.core.geometry.Vector2D;
import fag.core.model.Animation;
import fag.core.model.Frame;
import fag.core.model.Hitbox;
import fag.core.model.Model;

public class Editor extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	// Barre de menu
	JMenuBar menuBar = new JMenuBar();
	JMenu menu1 =  new JMenu("Model");
	JMenuItem menuItem10 = new JMenuItem("New");
	JMenuItem menuItem11 = new JMenuItem("Load");
	JMenuItem menuItem12 = new JMenuItem("Save");
	JMenu menu2 =  new JMenu("Image");
	JMenuItem menuItem20 = new JMenuItem("Load");
	JMenuItem menuItem21 = new JMenuItem("Delete");
	JFileChooser fileChooser = new JFileChooser();
	
	// Barre d'outils horizontale animation
	JToolBar haToolbar = new JToolBar("Horizontale : Animation");
	JLabel lbAnimation = new JLabel("Animation",JLabel.CENTER);
	Vector<String> vectorAnimation = new Vector<String>();
	JComboBox cbAnimation = new JComboBox(vectorAnimation);
	JButton btshowAnimation = new JButton("Show");
	JButton btplusAnimation = new JButton("Add");
	JButton btmoinsAnimation = new JButton ("Remove");
	JButton btrenameAnimation = new JButton("Rename");
	
	// Barre d'outils horizontale frame
	JToolBar hfToolbar = new JToolBar("Horizontale : Frame");
	JLabel lbFrame = new JLabel("Frame",JLabel.CENTER);
	Vector<String> vectorFrame = new Vector<String>();
	JComboBox cbFrame = new JComboBox(vectorFrame);
	JLabel vlbDuration = new JLabel("Duration(ms)",JLabel.CENTER);
	JFormattedTextField ftfDuration = new JFormattedTextField(NumberFormat.getIntegerInstance());
	JButton btDuration = new JButton("Set(ms)");
	JButton btshowFrame = new JButton("Show");
	JButton btplusFrame = new JButton("Add");
	JButton btmoinsFrame = new JButton ("Remove");
	
	// Barre d'outils horizontale Hitbox
	
	// Panel contenant les deux barres d'outils horizontales
	JPanel panelToolbar = new JPanel(new GridLayout(2,1));
	
	// Barre d'outils verticale gauche
	JToolBar vgToolbar = new JToolBar("Verticale gauche",SwingConstants.VERTICAL);
	ButtonGroup btGrpShapes = new ButtonGroup();
	JToggleButton tbtRectangle = new JToggleButton(
			new ImageIcon(getClass().getResource("/imgs/rectangle.png"), "Rectangle"));
	JToggleButton tbtCircle = new JToggleButton(
			new ImageIcon(getClass().getResource("/imgs/circle.png"), "Circle"));
	JToggleButton tbtPolygone = new JToggleButton(
			new ImageIcon(getClass().getResource("/imgs/polygone.png"), "Polygone"));
	
	// Barre d'outils verticale droite
	JToolBar vToolbar = new JToolBar("Verticale droite",SwingConstants.VERTICAL);
	JLabel vlbWidth = new JLabel("Width");
	JFormattedTextField ftfWidth = new JFormattedTextField(NumberFormat.getIntegerInstance());
	JLabel vlbHeight = new JLabel("Height");
	JFormattedTextField ftfHeight = new JFormattedTextField(NumberFormat.getIntegerInstance());
	JLabel vlbCotes = new JLabel("Nb Cotes");
	JFormattedTextField ftfCotes = new JFormattedTextField(NumberFormat.getIntegerInstance());
	
	// Serialisation
	Serializer serializer = new Serializer();
	
	// Zone de dessin
	DrawPanel drawPanel = new DrawPanel();
	
	// Variables
	String currentShape = "inconnue";
	
	public static void main(String[] args) {
		
		Editor editor = new Editor();
		
		// Initialisation d'une vue simple
		editor.newModel();
	}
	
	public Editor() {
		
		// Frame
		setTitle("Editor");
		setSize(1000,600);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setVisible(true);
		
		// Barre de menu
		menuItem10.addActionListener(this);
		menuItem11.addActionListener(this);
		menuItem12.addActionListener(this);
		menu1.add(menuItem10);
		menu1.add(menuItem11);
		menu1.add(menuItem12);
		menuItem20.addActionListener(this);
		menuItem21.addActionListener(this);
		menu2.add(menuItem20);
		menu2.add(menuItem21);
		menuBar.add(menu1);
		menuBar.add(menu2);
		
		// Barre d'outils horizontale Animation
		haToolbar.setFloatable(false);
		haToolbar.setLayout(new GridLayout(1,7));
		Font font = new Font("Arial",Font.BOLD,15);
		lbAnimation.setFont(font);
		haToolbar.add(lbAnimation);
		haToolbar.add(cbAnimation);
		haToolbar.add(btshowAnimation);
		haToolbar.add(btplusAnimation);
		haToolbar.add(btmoinsAnimation);
		haToolbar.add(btrenameAnimation);
		
		// Barre d'outils horizontale Frame
		hfToolbar.setFloatable(false);
		hfToolbar.setLayout(new GridLayout(1,9));
		lbFrame.setFont(font);
		hfToolbar.add(lbFrame);
		hfToolbar.add(cbFrame);
		hfToolbar.add(btshowFrame);
		hfToolbar.add(vlbDuration);
		hfToolbar.add(ftfDuration);
		hfToolbar.add(btDuration);
		hfToolbar.add(btplusFrame);
		hfToolbar.add(btmoinsFrame);
		
		// Panel contenant les deux barres d'outils horizontales
		panelToolbar.add(haToolbar);
		panelToolbar.add(hfToolbar);
		
		// Barre d'outils verticale gauche
		vgToolbar.setFloatable(false);
		btGrpShapes.add(tbtRectangle);
		btGrpShapes.add(tbtCircle);
		btGrpShapes.add(tbtPolygone);
		vgToolbar.add(tbtRectangle);
		vgToolbar.add(tbtCircle);
		vgToolbar.add(tbtPolygone);
		
		// Barre d'outils verticale droite
		vToolbar.setFloatable(false);
		vlbWidth.setVisible(false);
		ftfWidth.setVisible(false);
		vlbHeight.setVisible(false);
		ftfHeight.setVisible(false);
		vlbCotes.setVisible(false);
		ftfCotes.setVisible(false);
		vToolbar.add(vlbWidth);
		vToolbar.add(new JToolBar.Separator());
		vToolbar.add(ftfWidth);
		vToolbar.add(new JToolBar.Separator());
		vToolbar.add(vlbHeight);
		vToolbar.add(new JToolBar.Separator());
		vToolbar.add(ftfHeight);
		vToolbar.add(new JToolBar.Separator());
		vToolbar.add(vlbCotes);
		vToolbar.add(new JToolBar.Separator());
		vToolbar.add(ftfCotes);


		// Listeners
		SceneListener sceneListener = new SceneListener();
		drawPanel.addMouseListener(sceneListener);
		drawPanel.addMouseMotionListener(sceneListener);
		
		ToolBarShapeListener toolbarShapeListener = new ToolBarShapeListener();
		tbtRectangle.addMouseListener(toolbarShapeListener);
		tbtCircle.addMouseListener(toolbarShapeListener);
		tbtPolygone.addMouseListener(toolbarShapeListener);
		
		ToolBarAnimationListener toolbarAnimationListener = new ToolBarAnimationListener();
		btshowAnimation.addMouseListener(toolbarAnimationListener);
		btplusAnimation.addMouseListener(toolbarAnimationListener);
		btmoinsAnimation.addMouseListener(toolbarAnimationListener);
		btrenameAnimation.addMouseListener(toolbarAnimationListener);
		
		ToolBarFrameListener toolbarFrameListener = new ToolBarFrameListener();
		btshowFrame.addMouseListener(toolbarFrameListener);
		btplusFrame.addMouseListener(toolbarFrameListener);
		btmoinsFrame.addMouseListener(toolbarFrameListener);
		btDuration.addMouseListener(toolbarFrameListener);
		
		// Ajout des composants dans la fenetre
		setJMenuBar(menuBar);
		add(panelToolbar,BorderLayout.NORTH);
		add(drawPanel,BorderLayout.CENTER);
		add(vToolbar,BorderLayout.EAST);
		add(vgToolbar,BorderLayout.WEST);
		validate();
		repaint();	
	}

	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == menuItem10)
			newModel();
		if(e.getSource() == menuItem11)
			loadModel();
		if(e.getSource() == menuItem12)
			saveModel();
		if(e.getSource() == menuItem20)
			loadImage();
		if(e.getSource() == menuItem21)
			deleteImage();
	}
	
	public String explorer(String s) {
	
		String approve = new String(s);
		fileChooser.setCurrentDirectory(new File("."));
		int result = fileChooser.showDialog(fileChooser,approve);
		String myFile = null;
		
		if(result == JFileChooser.APPROVE_OPTION) {
			
			myFile = new String(fileChooser.getSelectedFile().toString());
		}
		return myFile;
	}

	public void newModel() {
		
		vectorAnimation.clear();
		vectorFrame.clear();
		vectorAnimation.add(0,"0");
		vectorAnimation.add(1,"Fin");
		cbAnimation.setSelectedIndex(0);
		vectorFrame.add(0,"0");
		vectorFrame.add(1,"Fin");
		cbFrame.setSelectedIndex(0);
		drawPanel.setCurrentAnimation(0);
		drawPanel.setCurrentFrame(0);
		drawPanel.setModel(new Model());
		drawPanel.getModel().getListAnimation().add(new Animation("0"));
		drawPanel.getModel().getListFrame(0).add(new Frame(0));
		drawPanel.getModel().getListFrame(0).get(0).getHitboxes().add(new Hitbox());
		drawPanel.setPosCenterX(drawPanel.getWidth()/2);
		drawPanel.setPosCenterY(drawPanel.getHeight()/2);
		validate();
		repaint();
	}
	
	public void loadModel() {
		
		String myFile = explorer("Load");
		drawPanel.setModel( (Model) serializer.Deserialize(myFile));
		vectorAnimation.clear();
		
		for(Animation a : drawPanel.getModel().getListAnimation()) {
			
			vectorAnimation.add(a.getName());
		}
		vectorAnimation.add("Fin");
		cbAnimation.updateUI();
		vectorFrame.clear();
		cbFrame.updateUI();
		repaint();
	}
	
	public void saveModel() {

		String myFile = explorer("Save");
		serializer.Serialize(drawPanel.getModel(),myFile);
	}
	
	public void loadImage() {
		
		String myFile = explorer("Load");
		BufferedImage tempImage = null;
		
		try {
			tempImage = ImageIO.read(new File(myFile));
			}
		catch (IOException e) {
			e.printStackTrace();
			}
		
		if(tempImage != null) {
			
			int curAnim = drawPanel.getCurrentAnimation();
			int curFrame = drawPanel.getCurrentFrame();
			
			if(curAnim > -1 && curFrame > -1) {
				
			drawPanel.getModel().getListFrame(curAnim).get(curFrame).setBufferedImage(tempImage);
			repaint();
			}
		}
	}
	
	public void deleteImage() {
		
		int curAnim = drawPanel.getCurrentAnimation();
		int curFrame = drawPanel.getCurrentFrame();
		Frame datFrame = drawPanel.getModel().getListFrame(curAnim).get(curFrame);
		
		datFrame.setBufferedImage(null);
		datFrame.setOffSet(new Vector2D());
		repaint();
	}
	
	public void resetSceneSelection() {
		
		currentShape = "inconnue";
		vlbWidth.setVisible(false);
		ftfWidth.setVisible(false);
		vlbHeight.setVisible(false);
		ftfHeight.setVisible(false);
		vlbCotes.setVisible(false);
		ftfCotes.setVisible(false);
		ftfWidth.setText("");
		ftfHeight.setText("");
		ftfCotes.setText("");
		btGrpShapes.clearSelection();
		
		
	}
	
	class ToolBarAnimationListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			
			int index = cbAnimation.getSelectedIndex();
			
			if(index > -1) {
				
				if(e.getSource() == btshowAnimation) {
					
					if(index != (-1 + vectorAnimation.size())) {
						
						drawPanel.setCurrentAnimation(index);
						vectorFrame.clear();
					
						for(int i = 0 ; i<drawPanel.getModel().getListFrame(index).size() ; i++) {

							vectorFrame.add(String.valueOf(i));
						}	
						vectorFrame.add("Fin");
						cbFrame.updateUI();
					}
				}	
				
				if(e.getSource() == btmoinsAnimation) {
								
					if(index != (-1 + vectorAnimation.size())) {
						
						vectorAnimation.remove(index);
						drawPanel.getModel().getListAnimation().remove(index);
						cbAnimation.updateUI();
						vectorFrame.clear();
						vectorFrame.add("Fin");
						cbFrame.updateUI();
						
						if(index == drawPanel.getCurrentAnimation()) {
							
							drawPanel.setCurrentAnimation(-1);
							repaint();
						}
					}
				}
				
				if(e.getSource() == btplusAnimation) {
					
						int val;
					
						for(int i=0 ; i<vectorAnimation.size()-1 ; i++) {
							
							val = Integer.parseInt(vectorAnimation.get(i));
							vectorAnimation.set(i,String.valueOf(val+1));
						}
						
						vectorAnimation.add(index,String.valueOf(index));
						drawPanel.getModel().getListAnimation().add(index,new Animation(String.valueOf(index)));
						drawPanel.getModel().getListFrame(index).add(0,(new Frame(0)));
						cbAnimation.updateUI();
						vectorFrame.clear();
						vectorFrame.add("0");
						vectorFrame.add("Fin");
						cbFrame.updateUI();
						repaint();
				}
				
				if(e.getSource() == btrenameAnimation) {
					
					if(index != (-1 + vectorFrame.size())) {
							
						String nom = JOptionPane.showInputDialog(null,
								 "Saisir le nom de l'Animation",
								 "Editor Popup",
								 JOptionPane.QUESTION_MESSAGE);
	
						if(nom != null && !nom.equals("")) {
							
							drawPanel.getModel().getListAnimation().get(index).setName(nom);
							vectorAnimation.set(index,nom);
							cbAnimation.updateUI();
						}
					}
				}
			}	
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
	
	class ToolBarFrameListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			
			int index = cbFrame.getSelectedIndex();
			
			if(index > -1) {
			
				if(e.getSource() == btshowFrame) {
					
					if(index != (-1 + vectorFrame.size())) {
						
						drawPanel.setCurrentFrame(index);
						ftfDuration.setText(String.valueOf(
								drawPanel.getModel().getListFrame(drawPanel.getCurrentAnimation())
									.get(index).getDuration()));
						ftfDuration.updateUI();
						repaint();
					}
				}
				
				if(e.getSource() == btmoinsFrame) {
					
					if(index != (-1 + vectorFrame.size())) {
						
						vectorFrame.remove(index);
						drawPanel.getModel().getListFrame(drawPanel.getCurrentAnimation()).remove(index);
					
						if(index == drawPanel.getCurrentFrame()) {
						
							drawPanel.setCurrentFrame(-1);
							repaint();
						}
					
						cbFrame.updateUI();
					}
				}
				
				if(e.getSource() == btplusFrame) {
						
						vectorFrame.add(index,String.valueOf(index));
						drawPanel.getModel().getListFrame(drawPanel.getCurrentAnimation())
						.add(index,new Frame(0));
						cbFrame.updateUI();
					}
				}
				
				if(e.getSource() == btDuration) {
					
					if(index != (-1 + vectorFrame.size())) {
						
						if(!(ftfDuration.getText().equals(""))) {
							
							drawPanel.getModel().getListFrame(drawPanel.getCurrentAnimation())
							.get(index).setDuration(Integer.parseInt(ftfDuration.getText()));	
						}
					}
				}
			}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
	
	class ToolBarShapeListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			
			if(e.getSource() == tbtRectangle) {
				
				tbtRectangle.setSelected(true);
				currentShape = "Rectangle";
				vlbWidth.setText("Width");
				vlbWidth.setVisible(true);
				ftfWidth.setText("");
				ftfWidth.setVisible(true);
				vlbHeight.setVisible(true);
				ftfHeight.setText("");
				ftfHeight.setVisible(true);
				vlbCotes.setVisible(false);
				ftfCotes.setVisible(false);
				repaint();
			}
			
			if(e.getSource() == tbtCircle) {
				
				tbtCircle.setSelected(true);
				currentShape = "Circle";
				vlbWidth.setText("Radius");
				vlbWidth.setVisible(true);
				ftfWidth.setText("");
				ftfWidth.setVisible(true);
				vlbHeight.setVisible(false);
				ftfHeight.setText("");
				ftfHeight.setVisible(false);
				vlbCotes.setVisible(false);
				ftfCotes.setVisible(false);
				repaint();
			}
			
			if(e.getSource() == tbtPolygone) {
				
				tbtPolygone.setSelected(true);
				currentShape = "Polygone";
				vlbWidth.setVisible(false);
				ftfWidth.setVisible(false);
				vlbHeight.setVisible(false);
				ftfHeight.setVisible(false);
				vlbCotes.setVisible(true);
				ftfCotes.setText("");
				ftfCotes.setVisible(true);
				repaint();
			}
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
		
	class SceneListener implements MouseListener, MouseMotionListener {
		
		boolean imagePressed = false;
		Rectangle2D rectPressed = null;
		Circle2D circlePressed = null;
		Polygon2D polygonPressed = null;
		
		int polygonStatus = 0;
		int nbCotes = 0;
		double clicX = 0;
		double clicY = 0;
		ArrayList<Vector2D> currentPoints = null;
		
		public void mouseClicked(MouseEvent e) {
			
			int curAnim = drawPanel.getCurrentAnimation();
			int curFrame = drawPanel.getCurrentFrame();
			Frame datFrame = drawPanel.getModel().getListFrame(curAnim).get(curFrame);
			
			if(curAnim > -1 && curFrame > -1) {
			
				if(e.getButton() == MouseEvent.BUTTON1) {
					
					if(currentShape == "Rectangle") {
											
						if(!ftfWidth.getText().equals("") && !ftfHeight.getText().equals("")) {
							
									
								Rectangle2D r = new Rectangle2D(e.getX()-drawPanel.getPosCenterX(),
															    e.getY()-drawPanel.getPosCenterY(),
															    Integer.parseInt(ftfWidth.getText()),
															    Integer.parseInt(ftfHeight.getText()));
								datFrame.getHitboxes().get(0).getShapes().add(r);
								resetSceneSelection();
								repaint();
								return;
						}
					}
					
					if(currentShape == "Circle") {
						
						if(!ftfWidth.getText().equals("")) {
							
									Circle2D c = new Circle2D(e.getX()-drawPanel.getPosCenterX(),
															  e.getY()-drawPanel.getPosCenterY(),
															  Integer.parseInt(ftfWidth.getText()));
								datFrame.getHitboxes().get(0).getShapes().add(c);
								resetSceneSelection();
								repaint();
								return;
						}
					}
					
					if(currentShape == "Polygone") {
						
						if(!ftfCotes.getText().equals("")) {
							
							if(polygonStatus == 0) {
								
								ftfCotes.setEditable(false);
								repaint();
								nbCotes = Integer.parseInt(ftfCotes.getText());
								currentPoints = new ArrayList<Vector2D>();
								currentPoints.add(new Vector2D(e.getX()-drawPanel.getPosCenterX(),
															   e.getY()-drawPanel.getPosCenterY()));
								polygonStatus++;
								return;
							}
						}	
						
							
						if(polygonStatus == (nbCotes-1)) {
			
							currentPoints.add(new Vector2D(e.getX()-drawPanel.getPosCenterX(),
									                       e.getY()-drawPanel.getPosCenterY()));
							Polygon2D currentPolygon = new Polygon2D(currentPoints);
							datFrame.getHitboxes().get(0).getShapes().add(currentPolygon);
							polygonStatus = 0;
							nbCotes = 0;
							currentPoints = null;
							ftfCotes.setEditable(true);
							resetSceneSelection();
							repaint();
							return;
						}

						if(polygonStatus > 0) {
							
							currentPoints.add(new Vector2D(e.getX()-drawPanel.getPosCenterX(),
									                       e.getY()-drawPanel.getPosCenterY()));
							polygonStatus++;
							return;
						}
					}
				}		
					
				if(e.getButton() == MouseEvent.BUTTON3) {
					
					for(Hitbox shape : datFrame.getHitboxes().get(0).getShapes()) {
					
						if(shape instanceof Rectangle2D) {
							
							Rectangle2D r = (Rectangle2D) shape;
							
							if(((drawPanel.getPosCenterX()+r.getX()) <= e.getX())
									&& (e.getX() <= (drawPanel.getPosCenterX()+r.getX()+r.getWidth()))
									&& ((drawPanel.getPosCenterY()+r.getY()) <= e.getY())
									&& (e.getY() <= (drawPanel.getPosCenterY()+r.getY()+r.getHeight()))) {
								
								datFrame.getHitboxes().get(0).getShapes().remove(r);
								repaint();
								return;
							}
						}
					
							if(shape instanceof Circle2D) {
								
								Circle2D c = (Circle2D) shape;
								if(Math.sqrt((Math.pow(e.getX()-(drawPanel.getPosCenterX()+c.getX()),2)
											+(Math.pow(e.getY()-(drawPanel.getPosCenterY()+c.getY()),2)))) 
										    < c.getRadius()) {
								
									datFrame.getHitboxes().get(0).getShapes().remove(c);
									repaint();
									return;
								}
							}
					
							if(shape instanceof Polygon2D) {
					
								Polygon2D p = (Polygon2D) shape;
								Rectangle2D r = p.getBounds();
					
								if(((drawPanel.getPosCenterX()+r.getX()) <= e.getX())
										&& (e.getX() <= (drawPanel.getPosCenterX()+r.getX()+r.getWidth()))
										&& ((drawPanel.getPosCenterY()+r.getY()) <= e.getY())
										&& (e.getY() <= (drawPanel.getPosCenterY()+r.getY()+r.getHeight()))) {
						
									datFrame.getHitboxes().get(0).getShapes().remove(p);
									repaint();
									return;
								}
							}
						}
					}
				}
			}
		
		public void mousePressed(MouseEvent e) {
			
			if(e.getButton() == MouseEvent.BUTTON1) {
			
				int curAnim = drawPanel.getCurrentAnimation();
				int curFrame = drawPanel.getCurrentFrame();
				Frame datFrame = drawPanel.getModel().getListFrame(curAnim).get(curFrame);
					
				for(Hitbox shape : datFrame.getHitboxes().get(0).getShapes()) {
					
					if(shape instanceof Rectangle2D) {
					
						Rectangle2D r = (Rectangle2D) shape;
						
						if(((drawPanel.getPosCenterX()+r.getX()) <= e.getX())
							&& (e.getX() <= (drawPanel.getPosCenterX()+r.getX()+r.getWidth()))
							&& ((drawPanel.getPosCenterY()+r.getY()) <= e.getY())
							&& (e.getY() <= (drawPanel.getPosCenterY()+r.getY()+r.getHeight()))) {
						
						rectPressed = r;
						return;
					}
				}
				
					if(shape instanceof Circle2D) {
					
						Circle2D c = (Circle2D) shape;
						
						if(Math.sqrt((Math.pow(e.getX()-(drawPanel.getPosCenterX()+c.getX()),2)
								+(Math.pow(e.getY()-(drawPanel.getPosCenterY()+c.getY()),2)))) 
							    < c.getRadius()) {
						
							circlePressed = c;
							return;
						}
					}
				
					if(shape instanceof Polygon2D) {
						
						Polygon2D p = (Polygon2D) shape;
						Rectangle2D r = p.getBounds();
						
						if(((drawPanel.getPosCenterX()+r.getX()) <= e.getX())
								&& (e.getX() <= (drawPanel.getPosCenterX()+r.getX()+r.getWidth()))
								&& ((drawPanel.getPosCenterY()+r.getY()) <= e.getY())
								&& (e.getY() <= (drawPanel.getPosCenterY()+r.getY()+r.getHeight()))) {
					
							polygonPressed = p;
							clicX = e.getX();
							clicY = e.getY();
							return;
						}
					}
				}
				
				if(datFrame.getBufferedImage() != null) {
			
					Rectangle2D r = new Rectangle2D(datFrame.getOffSet().x,
													datFrame.getOffSet().y,
													datFrame.getWidth(),
													datFrame.getHeight());
				
					if(((drawPanel.getPosCenterX()+r.getX()) <= e.getX())
						&& (e.getX() <= (drawPanel.getPosCenterX()+r.getX()+r.getWidth()))
						&& ((drawPanel.getPosCenterY()+r.getY()) <= e.getY())
						&& (e.getY() <= (drawPanel.getPosCenterY()+r.getY()+r.getHeight()))) {
						
						imagePressed = true;
						return;
					}
				}
				
			}
		}
	
		public void mouseDragged(MouseEvent e) {

				
				int curAnim = drawPanel.getCurrentAnimation();
				int curFrame = drawPanel.getCurrentFrame();
				Frame datFrame = drawPanel.getModel().getListFrame(curAnim).get(curFrame); 
			
				if(rectPressed != null) {
					
					rectPressed.setLocation(e.getX()-drawPanel.getPosCenterX(),
											e.getY()-drawPanel.getPosCenterY());
					repaint();
					return;
				}
				
				if(circlePressed != null) {
					
					circlePressed.setCenter(e.getX()-drawPanel.getPosCenterX(),
											e.getY()-drawPanel.getPosCenterY());
					repaint();
					return;
				}
				
				if(polygonPressed != null) {
					
					double translationX = e.getX()-clicX;
					double translationY = e.getY()-clicY;
					ArrayList<Vector2D> localList = (ArrayList<Vector2D>) polygonPressed.getVertices();
					
					for(Vector2D v : localList) {
						
						v.x = v.x+translationX;
						v.y = v.y+translationY;
					}
					
					datFrame.getHitboxes().get(0).getShapes().remove(polygonPressed);
					polygonPressed = new Polygon2D(localList);
					datFrame.getHitboxes().get(0).getShapes().add(polygonPressed);
					clicX = e.getX();
					clicY = e.getY();
					repaint();
					
				}
				
				if(imagePressed == true) {
					
					datFrame.setOffSet(new Vector2D(e.getX()-drawPanel.getPosCenterX(),
												   (e.getY()-drawPanel.getPosCenterY())));
					repaint();
					return;
				}
		}		

		
		
		public void mouseReleased(MouseEvent e) {
			
			if(e.getButton() == MouseEvent.BUTTON1) {
				
				if(rectPressed != null) {
					
					rectPressed = null;
					return;
				}
				
				if(circlePressed != null) {
					
					circlePressed = null;
					return;
				}
				
				if(polygonPressed != null) {
					
					polygonPressed = null;
					return;
				}
				
				if(imagePressed == true) {
					
					imagePressed = false;
					return;
				}
			}	
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
	}
}
